package com.flyu.service.bill;

import com.flyu.dao.BaseDao;
import com.flyu.dao.bill.BillDao;
import com.flyu.dao.bill.BillDaoImpl;
import com.flyu.pojo.Bill;
import com.flyu.pojo.Provider;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class BillServiceImpl implements BillService{
    // 业务层都会调用Dao层，所以引入UserDao
    private BillDao billDao;
    public BillServiceImpl() {
        billDao = new BillDaoImpl();
    }

    // 获取订单列表
    public List<Bill> getBillList(String productName, int providerId, int isPayment, int currentPageNo, int pageSize){
        Connection connection = null;
        List<Bill> billList = null;
        try {
            connection = BaseDao.getConnection();
            billList = billDao.getBillList(connection, productName, providerId, isPayment, currentPageNo, pageSize);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return billList;
    }
    // 根据查询条件统计记录总数
    public int getBillCount(String productName, int providerId, int isPayment){
        Connection connection = null;
        int billCount = 0;
        try {
            connection = BaseDao.getConnection();
            billCount = billDao.getBillCount(connection, productName, providerId, isPayment);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return billCount;
    }
    // 根据id删除订单
    public boolean delBillById(int billId){
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            int i = billDao.delBillById(connection, billId);
            if (i > 0){ // i > 0 说明删除成功
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
    // 根据id查询订单
    public Bill getBillById(int billId) {
        Connection connection = null;
        Bill bill = null;
        try {
            connection = BaseDao.getConnection();
            bill = billDao.getBillById(connection, billId);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return bill;
    }
    // 保存修改
    public boolean modifySave(Object[] params) {
         boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            int i = billDao.modifySave(connection, params);
            if (i > 0){     // 保存成功
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
    // 添加订单
    public boolean addBill(Bill bill) {
        boolean flag = false;

        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            int i = billDao.addBill(connection, bill);
            if (i > 0){
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }
    // 根据供应商id，查询对应的订单
    public List<Bill> getBillByProid(int proid) {
        List<Bill> billList = null;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            billList = billDao.getBillByProid(connection, proid);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return billList;
    }


    @Test
    public void test(){
        BillServiceImpl billService = new BillServiceImpl();
        List<Bill> billByProid = billService.getBillByProid(16);
        System.out.println(billByProid==null);
    }
}
